$(function() {
	layui.use(['form', "laydate"], function() {
		var form = layui.form;
		var laydate = layui.laydate;
		form.render();
		laydate.render({
			elem: "#birthday",
			type: "date"
		});

		function find_roleName_list() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/role/selectRoleName",
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						build_roleName_List(result);
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}

				}
			});
		}

		function build_roleName_List(result) {
			var roleNameList = result.data;
			$.each(roleNameList, function(index, item) {
				var roleCheckBox = $("<input type='checkbox' value='" + item.role_id + "' title='" + item.name + "'>");
				$(".roleList").append(roleCheckBox);
			});
			form.render();
		}
		find_roleName_list();

		$(".addUser").click(function() {
			check_role();
		var token=uuid(16,16);
			$("#token").val(token);
		});
		//绑定表带提交时间
		form.on("submit(addUser)", function(data) {
			console.log(data.field);
			var params=data.field;
			add_User(params);
			return false;
		});

		function check_role(){
			var i=0;			
			$("input[type='checkbox']").each(function(){
				if($(this).is(":checked")){
					$(this).attr("name","listUserRole["+i+"].fk_role_id");
					i++;
				}								
			});			
			form.render();
		}
		//想后天添加一条用户记录
		function add_User(params){
			$.ajax({
				type:"post",
				url:"http://localhost:8080/ssmService/sysUser/insertUserVo",
				async:true,
				data:params,
				"success":function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
						//获取当前弹出层，通过父页面获取当前inframe框的索引，级窗口名称
						var index = parent.layer.getFrameIndex(window.name);
						window.parent.location.reload();//刷新父页面
						parent.layer.close(index);//关闭当前页面

					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}
				},
				"error":function(result){
					layer.msg("该账号已经存在",{
						icon: 2,
						time: 1000
					});
				}
			});
		}		


		function uuid(len, radix) {
			var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
			var uuid = [],
				i;
			radix = radix || chars.length;

			if(len) {
				// Compact form
				for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
			} else {
				// rfc4122, version 4 form
				var r;

				// rfc4122 requires these characters
				uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
				uuid[14] = '4';

				// Fill in random data. At i==19 set the high bits of clock sequence as
				// per rfc4122, sec. 4.1.5
				for(i = 0; i < 36; i++) {
					if(!uuid[i]) {
						r = 0 | Math.random() * 16;
						uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
					}
				}
			}

			return uuid.join('');
		}

	});

});